package com.education.project.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import com.education.project.web.interceptor.SystemInterceptor;
import com.education.project.web.interceptor.WebInterceptor;

@Configuration
public class WebConfig implements WebMvcConfigurer {
	@Autowired
	private SystemInterceptor systemInterceptor;
	@Autowired
	private WebInterceptor webInterceptor;
	@Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(systemInterceptor)
			.addPathPatterns("/system/**");
		registry.addInterceptor(webInterceptor)
			.addPathPatterns("/user/**");
	}
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addRedirectViewController("/", "user/index");
	}
	
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		Map vars = new HashMap();
		vars.put("projectName", projectConfig.getName());
		vars.put("fileVersion", projectConfig.getFileVersion());
		vars.put("jqueryVersion", projectConfig.getJqueryVersion());
		vars.put("easyuiVersion", projectConfig.getEasyuiVersion());
		thymeleafViewResolver.setStaticVariables(vars);
		WebMvcConfigurer.super.configureViewResolvers(registry);
	}
}
